package com.loveshes.springbootstudy1.controller;

import com.loveshes.springbootstudy1.bean.vo.UserVO;
import com.loveshes.springbootstudy1.common.JsonData;
import com.loveshes.springbootstudy1.common.JsonResult;
import com.loveshes.springbootstudy1.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author WangZhe
 * @Date 2021-12-18 22:41
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("login")
    public JsonData<UserVO> login(UserVO userVO) {
        return JsonResult.<UserVO>success().data(userVO);
    }

    @RequestMapping("login2")
    public JsonData<UserVO> login2(@RequestBody UserVO userVO) {
        return JsonResult.<UserVO>success().data(userVO);
    }

    @RequestMapping("register")
    public JsonData register(@RequestBody UserVO userVO) {
        if (StringUtils.isAnyBlank(userVO.getUsername(), userVO.getPassword())) {
            return JsonResult.fail().message("参数缺失");
        }
        userService.insert(userVO);
        return JsonResult.success().message("注册成功");
    }

    @RequestMapping("register2")
    public JsonData register2(@RequestBody @Validated UserVO userVO) {
        userService.insertWithXml(userVO);
        return JsonResult.success().message("注册成功");
    }

}
